#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import datetime
import hashlib
import time
import base64
import os
import re
import sys
import json
import pandas
import chardet
import graphene
import config
from common.view import BaseApi
from res.lijiacai.utils import tool
from pandasticsearch import DataFrame


# from ....pandasticsearch import DataFrame


class AddQuestion(BaseApi, tool.OffLine2Es):
    name = "AddQuestion"
    description = "新增问卷"
    es_cluster = config.es_cluster

    class Argument:
        token = graphene.String(description="token", required=True)
        name = graphene.String(description="", required=True)
        registerPhone = graphene.String(description="", required=True)
        phone = graphene.String(description="", required=True)
        IDNumber = graphene.String(description="", required=True)
        city = graphene.String(description="", required=True)
        vehicleType = graphene.String(description="", required=True)
        days = graphene.String(description="", required=True)
        getStationName = graphene.String(description="", required=True)
        getTime = graphene.String(description="", required=True)
        returnStationName = graphene.String(description="", required=True)
        returnTime = graphene.String(description="", required=True)
        createTime = graphene.String(description="", required=True)

    class Return:
        succ = graphene.Boolean()

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        token = self.arguments.get("condition", {}).get("token")
        self.deal_token(token)
        name = self.arguments.get("condition", {}).get("name")
        registerPhone = self.arguments.get("condition", {}).get("registerPhone")
        phone = self.arguments.get("condition", {}).get("phone")
        IDNumber = self.arguments.get("condition", {}).get("IDNumber")
        city = self.arguments.get("condition", {}).get("city")
        vehicleType = self.arguments.get("condition", {}).get("vehicleType")
        days = self.arguments.get("condition", {}).get("days")
        getStationName = self.arguments.get("condition", {}).get("getStationName")
        getTime = self.arguments.get("condition", {}).get("getTime")
        returnStationName = self.arguments.get("condition", {}).get("returnStationName")
        returnTime = self.arguments.get("condition", {}).get("returnTime")
        createTime = self.arguments.get("condition", {}).get("createTime")
        data_ = self.arguments.get("condition", {})
        try:
            del data_["token"]
        except:
            pass
        if not createTime:
            data_.update({"createTime": str(datetime.datetime.now())})
        self.data = [data_]
        self.update(sql="", sql_fields=[], index=config.long_rental_question_dataset)
        return {"succ": True}


class SearchQuestion(BaseApi):
    name = "SearchQuestion"
    description = "查询问卷"
    es_cluster = config.es_cluster

    class Argument:
        token = graphene.String(description="token", required=True)
        name = graphene.String(description="")
        registerPhone = graphene.String(description="")
        phone = graphene.String(description="")
        IDNumber = graphene.String(description="")
        city = graphene.String(description="")
        vehicleType = graphene.String(description="")
        days = graphene.String(description="")
        getStationName = graphene.String(description="")
        getTime_start = graphene.String(description="")
        getTime_end = graphene.String(description="")
        returnStationName = graphene.String(description="")
        returnTime_start = graphene.String(description="")
        returnTime_end = graphene.String(description="")
        createTime_start = graphene.String(description="")
        createTime_end = graphene.String(description="")

    class Return:
        class SearchQuestion1(graphene.ObjectType):
            name = graphene.String(description="")
            registerPhone = graphene.String(description="")
            phone = graphene.String(description="")
            IDNumber = graphene.String(description="")
            city = graphene.String(description="")
            vehicleType = graphene.String(description="")
            days = graphene.String(description="")
            getStationName = graphene.String(description="")
            getTime = graphene.String(description="")
            returnStationName = graphene.String(description="")
            returnTime = graphene.String(description="")
            createTime = graphene.String(description="")

        rows = graphene.List(SearchQuestion1)

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        token = self.arguments.get("condition", {}).get("token")
        self.deal_token(token)
        name = self.arguments.get("condition", {}).get("name")
        registerPhone = self.arguments.get("condition", {}).get("registerPhone")
        phone = self.arguments.get("condition", {}).get("phone")
        IDNumber = self.arguments.get("condition", {}).get("IDNumber")
        city = self.arguments.get("condition", {}).get("city")
        vehicleType = self.arguments.get("condition", {}).get("vehicleType")
        days = self.arguments.get("condition", {}).get("days")
        getStationName = self.arguments.get("condition", {}).get("getStationName")
        getTime_start = self.arguments.get("condition", {}).get("getTime_start")
        getTime_end = self.arguments.get("condition", {}).get("getTime_end")
        returnStationName = self.arguments.get("condition", {}).get("returnStationName")
        returnTime_start = self.arguments.get("condition", {}).get("returnTime_start")
        returnTime_end = self.arguments.get("condition", {}).get("returnTime_end")
        createTime_start = self.arguments.get("condition", {}).get("createTime_start")
        createTime_end = self.arguments.get("condition", {}).get("createTime_end")
        condition = False
        df = DataFrame.from_es(url=self.es_cluster[0], index=config.long_rental_question_dataset, doc_type="doc",
                               compat=6)
        if name:
            if not condition:
                condition = (df["name.keyword"] == name)
            else:
                condition = condition & (df["name.keyword"] == name)
        if registerPhone:
            if not condition:
                condition = (df["registerPhone.keyword"] == registerPhone)
            else:
                condition = condition & (df["registerPhone.keyword"] == registerPhone)
        if phone:
            if not condition:
                condition = (df["phone.keyword"] == phone)
            else:
                condition = condition & (df["phone.keyword"] == phone)
        if IDNumber:
            if not condition:
                condition = (df["IDNumber.keyword"] == IDNumber)
            else:
                condition = condition & (df["IDNumber.keyword"] == IDNumber)
        if city:
            if not condition:
                condition = (df["city.keyword"] == city)
            else:
                condition = condition & (df["city.keyword"] == city)
        if vehicleType:
            if not condition:
                condition = (df["vehicleType.keyword"] == vehicleType)
            else:
                condition = condition & (df["vehicleType.keyword"] == vehicleType)
        if days:
            if not condition:
                condition = (df["days.keyword"] == days)
            else:
                condition = condition & (df["days.keyword"] == days)
        if getStationName:
            if not condition:
                condition = (df["getStationName.keyword"] == getStationName)
            else:
                condition = condition & (df["getStationName.keyword"] == getStationName)

        if returnStationName:
            if not condition:
                condition = (df["returnStationName.keyword"] == returnStationName)
            else:
                condition = condition & (df["returnStationName.keyword"] == returnStationName)
        if getTime_start:
            if not condition:
                condition = (df["getTime"] >= getTime_start)
            else:
                condition = condition & (df["getTime"] >= getTime_start)
        if getTime_end:
            if not condition:
                condition = condition & (df["getTime"] <= getTime_end)
            else:
                condition = condition & (df["getTime"] <= getTime_end)
        if returnTime_start:
            if not condition:
                condition = (df["returnTime"] >= returnTime_start)
            else:
                condition = condition & (df["returnTime"] >= returnTime_start)
        if returnTime_end:
            if not condition:
                condition = (df["returnTime"] <= returnTime_end)
            else:
                condition = condition & (df["returnTime"] <= returnTime_end)
        if createTime_start:
            if not condition:
                condition = (df["createTime"] >= createTime_start)
            else:
                condition = condition & (df["createTime"] >= createTime_start)
        if createTime_end:
            if not condition:
                condition = (df["createTime"] <= createTime_end)
            else:
                condition = condition & (df["createTime"] <= createTime_end)

        df = df.filter(condition=condition)
        try:
            res = df.to_pandas().to_dict(orient="record")
        except:
            res = []
        # print(res)
        return {"rows": res}
